package com.example.linkedlist;

public class LinkedQueue<T> implements Queue<T> {
    private int count=0;
    private LinearNode1<T> front,rear;

    @Override
    public void enqueue(T element) {
        LinearNode1<T> n=new LinearNode1(element);

        if (isEmpty()){
            front=n;
        }
        else {
            rear.setNext(n);
        }
        rear=n;
        count++;
    }

    @Override
    public T dequeue() {
        if (isEmpty()){
            throw new EmptyCollectionException("Chap15.Queue");
        }
        else {
            LinearNode1<T> n=front;
            front=front.getNext();
            count--;

            if (isEmpty()){
                rear=null;
            }

            return n.getElement();
        }

    }

    @Override
    public T first() {
        return front.getElement();
    }

    @Override
    public boolean isEmpty() {
        if (count==0){
            return true;
        }
        else {
            return false;
        }
    }

    @Override
    public int size() {
        return count;
    }

    @Override
    public String toString() {
        String string="";
        LinearNode1 temp=front;

        if (temp==null){
            throw new EmptyCollectionException("Chap15.Queue");
        }
        else if (temp.getNext()==null){
            return ""+temp.getElement();
        }
        else {
            while (temp!=null){
                string=string+temp.getElement()+" ";
                temp=temp.getNext();
            }
            return string;
        }
    }


    public void changeelement(int element,int locate){
        LinearNode1 head=front;
        for (int i=1;i<locate;i++){
            head=head.getNext();
        }
        head.setElement(element);
    }

    public void  change(int locate1,int locate2){
        LinearNode1 head=front;
        LinearNode1<Integer> temp1=head,temp2=head;
        int temp;
        for (int i=1;i<locate1;i++){
            temp1=temp1.getNext();
        }
        for (int i=1;i<locate2;i++){
            temp2=temp2.getNext();
        }
        temp=temp1.getElement();
        temp1.setElement(temp2.getElement());
        temp2.setElement(temp);
    }

    public void reverse(){
        LinearNode1 head=front;
        Integer[] temp=new Integer[size()];
        for (int i=size()-1;i>=0;i--){
            temp[i]= (Integer) head.getElement();
            head=head.getNext();
        }
        head=front;
        for (int i=0;i<size();i++){
            head.setElement(temp[i]);
            head=head.getNext();
        }
    }
}
